/*
 * DirectoryChooser.java
 * 
 * last update: 14.01.2010 by Stefan Saru
 * 
 * author:	Lucian Ghita Felix(felix@ulise.cs.pub.ro)
 *
 * Obs: N/A
 */
package ide;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;


/**
 * This dialog allows users to choose a directory
 * @version 1.0
 * @author fEliX
 */
@SuppressWarnings("serial")
public class DirectoryChooser extends JDialog {

	JButton browseButton;
	JTextField directoryEdit;

	public String selectedDirectory;

	public DirectoryChooser(Dialog parent, String startDir) {
		super(parent, "Gimme a directory", true);  //modal

		directoryEdit = new JTextField(30);  // 30 columns
		selectedDirectory = startDir != null ? new String(startDir) : new String(" ");

		try { 
			directoryEdit.getDocument().remove(0, directoryEdit.getDocument().getLength());
			directoryEdit.getDocument().insertString(0, startDir, null);
		} catch (javax.swing.text.BadLocationException e) {
			System.out.println("Exception " + e.toString());
		} 
		JPanel panel = new JPanel(new BorderLayout());

		browseButton = new JButton(".."); 
		browseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BrowsePressed();
			}});



		panel.add(directoryEdit, BorderLayout.CENTER);
		panel.add(browseButton, BorderLayout.EAST);


		JPanel buttonPanel = new JPanel();

		buttonPanel.setLayout ( new FlowLayout(FlowLayout.RIGHT) );
		JButton cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CancelPressed();
			}});
		buttonPanel.add( cancel );

		JButton ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				OKPressed();
			}});
		buttonPanel.add( ok );
		getRootPane().setDefaultButton(ok);     

		panel.add(buttonPanel, BorderLayout.SOUTH);


		getContentPane().add(panel);
	}



	public String getDirectory() {
		this.show();
		return selectedDirectory; 

	}

	private void CancelPressed() {
		selectedDirectory = null;
		this.setVisible(false);
	}

	private void OKPressed() {
		try {
			selectedDirectory = directoryEdit.getDocument().getText(0, directoryEdit.getDocument().getLength()).trim();
		} catch (javax.swing.text.BadLocationException e) {
			System.out.println("Exception " + e.toString());
		}


		File dir = new File(selectedDirectory);
		// now we do some chekcs to see if the user is still awake

		if (!dir.exists()) {
			JOptionPane.showMessageDialog(null, "This stuff you typed points to something nonexistent.\nPlease try again!\n..or give it up..", "Input error", JOptionPane.ERROR_MESSAGE); 
			return;       
		}

		if (!dir.isDirectory()) {
			JOptionPane.showMessageDialog(null, "The stuff you typed is not a directory", "Input error", JOptionPane.ERROR_MESSAGE); 
			return;       
		}

		this.setVisible(false);
	}

	private void BrowsePressed() {

		JFileChooser chooser = new JFileChooser();
		chooser.setDialogTitle("Selecty directory:");
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);


		//VideFileFilter vfilter = new VideFileFilter("v", "Verilog source file");
		//chooser.setFileFilter(vfilter);

		int returnVal = chooser.showDialog(this, "Select");
		if(returnVal != JFileChooser.APPROVE_OPTION) return;
		String dirName;

		try {
			dirName = chooser.getSelectedFile().getCanonicalPath();
		} catch (java.io.IOException e) {
			System.out.println("Huh?! :" + e.toString());
			dirName = new String("Huh?!");
		}

		//we put in textField what we get from FileDialog.
		try {
			directoryEdit.getDocument().remove(0, directoryEdit.getDocument().getLength());
			directoryEdit.getDocument().insertString(0, dirName, null);
		} catch (javax.swing.text.BadLocationException e) {
			System.out.println("Exception " + e.toString());
		}


	}

}
